package com.google.code.sharecontent.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.google.code.sharecontent.dao.ResourceDao;
import com.google.code.sharecontent.model.Resource;

public class ResourceDaoImpl extends BaseDaoImpl<Resource> implements ResourceDao {

	public ResourceDaoImpl(Connection conn) {
		super(conn);
	}


	@Override
	public Resource createByRow(ResultSet rs) throws SQLException {
		Resource status = new Resource();
		status.setId(rs.getInt("ID"));
		status.setName(rs.getString("NAME"));
		status.setUrl(rs.getString("URL"));
		status.setStatusId(rs.getInt("STATUS_ID"));
		status.setUserId(rs.getInt("USER_ID"));
		return status;
	}

	@Override
	protected String generateInsertSql(Resource status) {
		return "insert into "+getTableName()+" (NAME, URL, STATUS_ID, USER_ID) values (?, ?, ?, ?)";
//		return "insert into "+getTableName()+" (NAME, URL, STATUS_ID, USER_ID) values ('"+status.getName()+"','"+status.getUrl()+"',"+status.getStatusId()+","+status.getUserId()+")";
	}
	
	@Override
	protected String generateUpdateSql(Resource status) {
		return "update "+getTableName()+" set NAME = ?,URL = ?," +
				"STATUS_ID = ?,USER_ID = ? "+
		" where ID = "+ status.getId();
	}
	
	@Override
	protected String getTableName() {
		return "RESOURCES";
	}


	@Override
	protected PreparedStatement putPrepareStatement(PreparedStatement pStat,
			Resource status) throws SQLException {
		pStat.setString(1, status.getName());
		pStat.setString(2, status.getUrl());
		pStat.setInt(3, status.getStatusId());
		pStat.setInt(4, status.getUserId());
		return pStat;
	}

}
